package com.tank.action;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.tank.bean.MailValid;
import com.tank.bean.User;
import com.tank.core.TankModel;
import com.tank.service.MailService;
import com.tank.vo.RetVo;

@Controller
@Scope("prototype")
public class ValidAction extends ActionSupport {

	@Resource(name="mailService")
	MailService service;

	MailValid mv;
	User user;
	
	TankModel tm;
	
	
	// valid?mv.action=fjwofjewofijwoefijoefjowejfeofe
	// mv.uuid = mv.action;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public TankModel getTm() {
		return tm;
	}

	public void setTm(TankModel tm) {
		this.tm = tm;
	}

	public MailValid getMv() {
		return mv;
	}


	public void setMv(MailValid mv) {
		this.mv = mv;
	}

	@Override
	public String execute() throws Exception {
		 
		 
		mv=service.findMailValid(mv);
		 
		 if(mv != null){
		 
			 if (MailValid.VALID_REG.equals(mv.getAction())) {
				tm=service.regValid(mv);
			 }else if(MailValid.VALID_FIND.equals(mv.getAction())){
				 tm=service.resetValid(mv);
			 }
		 }else{
			 tm=TankModel.newInstance();
			 tm.setSuccess(false);
			 tm.setMessage("验证失败！请连系站长admin@qq.com");
		 }
		 return "show";
	}
	
	

	public String resetForm() throws Exception {
		
		if(user == null) return INPUT;//到表单界面
		if(user.getMail() != null && user.getMail().matches("\\w+@\\w+\\.\\w+")){
			tm=service.resetForm(user);
			if(tm.isSuccess()){
			return SUCCESS;
			}
			
			return INPUT;
		}
		
		tm =TankModel.newInstance();
		tm.setSuccess(false);
		tm.setMessage("邮箱格式有错！（例：abc@qq.com）");
		
		tm.printSuccess();
		
		user.setMail(null);
		
		return INPUT;
	}
}
